import router from "@/router"
import {setCookie,getCookie,removeCookie} from 'utils/cookie.js'

export default {
  namespaced:true,
  state:{
    list:[], //原始list数据
    menuList:[],
    rootSubmenuKeys: [],
    openKeys: [],
    selectedKeys:[],
    tagList:[{id:1,name:'home',path:'/admin',title:'首页',isCheck:false}]
  },
  mutations:{
    setOpenKeys(state,key){
      state.openKeys = key
    },
    closeTag(state,name){
      let tagList = state.tagList
      let idx = tagList.findIndex(e=>e.name==name)
      if(tagList[idx].isCheck){
        if(tagList[idx+1]){
          router.push({
            path:tagList[idx+1].path
          })
        }else{
          router.push({
            path:tagList[idx-1].path
          })
        }
      }
      tagList.splice(idx,1)
      state.tagList = tagList
      this.commit('menu/saveTag')
    },
    closeAll(state){
      state.tagList = [{id:1,name:'home',path:'/admin',title:'首页',isCheck:true}]
      this.commit('menu/saveTag')
    },
    closeOther(state){
      let tagList = state.tagList
      let arr = []
      let home = tagList.find(e=>e.name=='home')
      arr.push(home)
      if(!home.isCheck){
        let i = tagList.find(e=>e.isCheck)
        arr.push(i)
      }
      state.tagList = arr
      this.commit('menu/saveTag')
    },
    saveTag(state){
      let tagList = state.tagList
      // removeCookie('tagList')
      setCookie('tagList',tagList)
    },
  },
  actions:{
    async start(context,path){
      await context.dispatch('getMenuData') //请求menu数据入口
      context.dispatch('dealMenu',path)
    },
    dealMenu(context,path){
      //配对path确认selectedKeys
      let f = context.state.list.find(e=>e.path==path||e.path+'/'==path)
      if(!f){return false}
      // console.log(f)
      if(f.pid != 0){
        let p = context.state.list.find(e=>e.id == f.pid)
        if(p.pid != 0){
          let pp = context.state.list.find(e=>e.id == p.pid)
          context.state.selectedKeys = [f.key,p.key,pp.key]
          context.state.openKeys = [p.key,pp.key]
        }else{
          context.state.selectedKeys = [f.key,p.key]
          context.state.openKeys = [p.key]
        }
      }else{
        context.state.selectedKeys = [f.key]
        context.state.openKeys = []
      }
      context.dispatch('dealTag',{id:f.id,path:f.path,name:f.name,title:f.title})
    },
    dealTag(context,data){
      // tagList中无path则新增，有且未选中，则ischeck-true,else不操作
      let tagList = context.state.tagList
      let f = tagList.find(e=>e.path==data.path||e.path+'/'==data.path)
      for(let i of tagList){
        i.isCheck = false
      }
      if(f){
        if(!f.isCheck){
          f.isCheck = true
        }
      }else{
        data.isCheck = true
        context.state.tagList.push(data)
      }
      setCookie('tagList',tagList)
    },
    getMenuData(context){
      return new Promise((resolve, reject) => {
        // 你的请求
        let D = dealList(staticData)
        context.state.list = D.list
        context.state.menuList = D.menuList
        context.state.rootSubmenuKeys = D.rootSubmenuKeys
        // 获取cookie赋值tagList
        // (待添加)与list进行比对，防止is_show==true情况下还显示tag
        let tagList = getCookie('tagList')
        context.state.tagList = JSON.parse(tagList) 
        resolve()
      });
    },
    
  }
}

const staticData = [
  {id:1,pid:0,title:'首页',path:'/admin',name:'home',icon:'HomeOutlined'},
  {id:2,pid:0,title:'文章',path:'',name:'article',icon:'CopyOutlined'},
  {id:3,pid:2,title:'文章列表',path:'/admin/article_list',name:'article_list'},
  {id:4,pid:2,title:'文章分类',path:'/admin/article_classify',name:'article_classify'},
  {id:10,pid:0,title:'商品',path:'',name:'product',icon:'ShoppingOutlined'},
  {id:11,pid:10,title:'商品管理',path:'/admin/product_list',name:'product_list'},
  {id:12,pid:10,title:'商品分类',path:'/admin/product_classify',name:'product_classify'},
  {id:13,pid:10,title:'商品评论',path:'/admin/product_reply',name:'product_reply'},
  {id:14,pid:10,title:'商品规格',path:'/admin/product_spec',name:'product_spec'},
  {id:5,pid:0,title:'设置',path:'',name:'setting',icon:'SettingOutlined'},
  {id:6,pid:5,title:'配置管理',path:'',name:'system',icon:''},
  {id:7,pid:6,title:'栏目配置',path:'/admin/system_menus',name:'system_menus'},
  {id:8,pid:6,title:'管理员配置',path:'/admin/system_admin',name:'system_admin'},
  {id:9,pid:5,title:'系统设置',path:'/admin/config',name:'config'},
]

function dealList(data){
  let menuList = []
  let rootSubmenuKeys = []
  for(let i of data){
    i.children = data.filter(e=>e.pid == i.id)
    i.key = i.pid==0?'sub'+i.id:'sub'+i.pid+'-'+i.id
    if(i.pid == 0){
      rootSubmenuKeys.push(i.key)
      menuList.push(i)
    }
  }
  console.log({menuList,rootSubmenuKeys,list:data})
  return {menuList,rootSubmenuKeys,list:data}
}